package xml;

import java.util.ArrayList;
import java.util.LinkedHashMap;


import org.apache.xerces.parsers.SAXParser;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class XmlStatisticsHandler extends DefaultHandler{
	private LinkedHashMap<String,Integer> _labelsToIdMap = new LinkedHashMap<String,Integer>();
	private LinkedHashMap<ArrayList<Integer>,Integer> _routsToIdMap = new LinkedHashMap<ArrayList<Integer>,Integer>();
	private LinkedHashMap<ArrayList<Integer>,Integer> _nodesOfRoutes = new LinkedHashMap<ArrayList<Integer>, Integer>();
	
	private int _nodeCount = 0;
	
	private ArrayList<Integer> _currentRoute = new ArrayList<Integer>();
	
	public XmlStatisticsHandler()
	{
		
	}
	
	
	public void startElement(String uri, String localName, String qName, Attributes attributes)  
	{
		_nodeCount++;
		if (!_labelsToIdMap.containsKey(qName))
		{
			int labelId = _labelsToIdMap.size();
			_labelsToIdMap.put(qName, labelId);
			_currentRoute.add(labelId);
		}
		else
		{
			_currentRoute.add(_labelsToIdMap.get(qName));
		}
		
		if (!_routsToIdMap.containsKey(_currentRoute))
		{
			ArrayList<Integer> route = new ArrayList<Integer>(_currentRoute);
			_routsToIdMap.put(route,_routsToIdMap.size());
			_nodesOfRoutes.put(route,1);
		}
		else
		{
			_nodesOfRoutes.put(_currentRoute,_nodesOfRoutes.get(_currentRoute)+1);
		}
		//PrintRouteLabels(_currentRoute);
		
		if (_nodeCount % 1000000 == 0)
		{
			System.out.println("first pass " + _nodeCount);
		}
	}
	 
	public void endElement(String uri, String localName,String qName) throws SAXException 
	{
		
		//trim the last id from the current path
		_currentRoute.remove(_currentRoute.size()-1);
		
	}
	
	public int GetNumberOfNodes()
	{
		return _nodeCount;
	}
	
	private void PrintRouteLabels(ArrayList<Integer> route)
	{
		ArrayList<String> l = new ArrayList<String>();
		String[] strArr = new String[_labelsToIdMap.keySet().size()];
		
		_labelsToIdMap.keySet().toArray(strArr);
		
		for(int i : route)
		{
			l.add(strArr[i]);
		}
		
		System.out.println(l);
	}
	public LinkedHashMap<ArrayList<Integer>,Integer> GetRouts()
	{
		/*ArrayList<ArrayList<Integer>> routesList = new ArrayList<ArrayList<Integer>>();
		
		Iterator<ArrayList<Integer>> routesIter = _routs.iterator();
		
		while(routesIter.hasNext())
		{
			routesList.add(routesIter.next());
		}
		
		return routesList;*/
		
		return _routsToIdMap;
	}
	
	public LinkedHashMap<ArrayList<Integer>,Integer> GetNodesOfRoutes()
	{
		return _nodesOfRoutes;
	}
	
	public LinkedHashMap<String,Integer> GetLabels()
	{
		/*ArrayList<String> labelsList = new ArrayList<String>();
		for(String l : _labelsToIdMap.keySet())
		{
			labelsList.add(l);
		}
		
		return labelsList;*/
		
		return _labelsToIdMap;
	}
	
	
	public static void main(String[] args) throws Exception
	{
		SAXParser saxParser = new SAXParser();
		XmlStatisticsHandler handler = new XmlStatisticsHandler();
		saxParser.setContentHandler(handler);
		//saxParser.parse(xmlPath, handler);
		saxParser.parse("D:\\development\\dblab\\xmlDocs\\paperTest.xml");
		
		System.out.println(handler.GetNumberOfNodes());
		System.out.println(handler.GetLabels().size());
		System.out.println(handler.GetRouts().size());
	}

}
